#ifndef NETWORKCLIENT_H
#define NETWORKCLIENT_H

#include <QObject>
#include <QTcpSocket>
#include <QJsonObject>
#include "protocol.h"

class NetworkClient : public QObject {
    Q_OBJECT
public:
    explicit NetworkClient(QObject *parent = nullptr);
    ~NetworkClient();
    
    static NetworkClient& instance();
    
    // 设置默认服务器配置
    void setDefaultServer(const QString& host, quint16 port);
    QString defaultHost() const { return m_defaultHost; }
    quint16 defaultPort() const { return m_defaultPort; }
    
    bool connectToServer(const QString& host, quint16 port);
    void disconnectFromServer();
    bool isConnected() const;
    
    void sendRequest(MessageType type, const QJsonObject& data);
    
signals:
    void connected();
    void disconnected();
    void messageReceived(MessageType type, const QJsonObject& data);
    void errorOccurred(const QString& error);
    
private slots:
    void onConnected();
    void onDisconnected();
    void onReadyRead();
    void onError(QAbstractSocket::SocketError error);
    
private:
    QTcpSocket* m_socket;
    QByteArray m_buffer;
    QString m_defaultHost;
    quint16 m_defaultPort;
};

#endif // NETWORKCLIENT_H

